<?php
/**
 * 创建 全品类热度飙升榜
 * 更新时间：每周一，3点41
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandRankingBrand as bigBrandRankingBrand;
use Biz\model\bigData\BrandFullCateRankingPublic;
use Biz\model\wiki\WikiRankingCatePublic;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;
use Biz\tools\Mail;
use Biz\model\brand\Brand as mBrand;
use Biz\model\brand\Content as mBrandContent;
use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Doraemon\cache\BlankCounter;
use Doraemon\cache\PhotoCounter;
use Doraemon\model\AdmUserType;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;
use Biz\service\wikiRanking\AdmRanking;

$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$sourceDay = $dayArr['source_day'];
$now = date('Ymd');
if ($sourceDay != $now) {
    var_dump('当前日期为：' . $now . '不能创建：' . $sourceDay . '的榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}

$list = [];
$bigData = BrandFullCateRankingPublic::select('*', ['day' => $sourceDay,
    'ORDER' => ['total_score' => 'DESC'] , 'LIMIT' => [0,400]]);
if (empty($bigData)) {
    var_dump('未查询到榜单数据');
    Mail::send(['tianhuining@haohaozhu.com', 'caowenxue@haohaozhu.com'], '未查询到榜单数据', '未查询到榜单数据#sql:' . json_encode(BrandFullCateRankingPublic::log()));
    return false;
}
$publicRankingList = [];
$bigBrandCount = [];
foreach ($bigData as  $row) {
    if($bigBrandCount[$row['brand_id']] < 2){
        $bigBrandCount[$row['brand_id']] += 1;
        $list[] = $row;
    }
}
$list = array_slice($list ,0 , 100);
//获取picId
$basic_pic_list = AdmRanking::getBasicPicIdsByRankingList($list);
//获取picId
foreach ($list as $row) {
    $obj_id = getWikiBasicObj($day ,$row['basic_id'] );
    $obj_type = IdParse::getType($obj_id);
    if($obj_id ){
        $data = [
            'ranking_day' => $day,
            'brand_id' => $row['brand_id'],
            'score' => $row['total_score'],
            'obj_id' => $obj_id,
            'obj_type' => $obj_type,
            'basic_id' => $row['basic_id'],
            'basic_pic_id' => $basic_pic_list[$row['basic_id']] ? : '',
            'addtime' => date('Y-m-d H:i:s'),
            'edittime' => date('Y-m-d H:i:s'),
        ];
        WikiRankingCatePublic::insert($data);
    }
   // var_dump('创建「' . $rankingName . '」' . '#' . $rankingId . '的品牌榜单');

}
$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,创建榜单-全品类口碑榜,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');


function getWikiBasicObj($day, $basicId)
{
    $objId = '';
    $lastWeekInfo = WikiRankingWeek::getByWhere(['ranking_day[<]' => $day, 'ORDER' => ['ranking_day' => 'DESC']]);
    if (!empty($lastWeekInfo)) {
        $lastDay = $lastWeekInfo['ranking_day'];
        $lastInfo = WikiRankingCatePublic::getByWhere(['ranking_day' => $lastDay, 'basic_id' => $basicId, 'is_changed_obj' => 1, 'obj_id[!]' => '']);
        if (!empty($lastInfo)) {
            var_dump('basic Id:' . $basicId . '#查询到上一期：' . $lastDay . '人工设置内容id:' . $objId);
            $objId = $lastInfo['obj_id'];
            return $objId;
        }
    }

    $basicInfo = mWikiBasic::getByWhere(['id' => $basicId]);
    $brandId = $basicInfo ? $basicInfo['brand_id'] : 0;
    $wikiInfos = mWiki::selectByWhere(['basic_id' => $basicId, 'status[!]' => 9]);
    $wikiIds = $wikiInfos ? array_column($wikiInfos, 'id') : [];
    if ($wikiIds) {
        //wiki 关联的note
        $objIds = mObjRelateWiki::getByWhere(['wiki_id' => $wikiIds, 'obj_type' => [mObjRelateWiki::OBJ_TYPE_NOTE, mObjRelateWiki::OBJ_TYPE_BLANK], 'status' => mObjRelateWiki::STATUS_NORMAL], 'obj_id');
        if ($objIds) {
            $brandUid = $brandId ? mBrand::getBrandUidById($brandId) : 0;
            if ($brandId) {
                $objUid = IdParse::getUid($objId);
                //查询被品牌商设为精选的内容
                $objId = mBrandContent::get('obj_id', ['brand_id' => $brandId, 'obj_id' => $objIds, 'is_selected' => 1]);
                //品牌自己的内容不能设置为口碑内容
                if ($objUid && $brandUid && $objUid == $brandUid) {
                    $objId = '';
                }
            }
            if (!$objId) {
                $noteIds = [];
                $blankIds = [];
                foreach ($objIds as $val) {
                    $objType = IdParse::getType($val);
                    $objUid = IdParse::getUid($val);
                    $isBelevel = AdmUserType::isBlevelsByuid($objUid);
                    if (!$isBelevel) {//不是B级用户
                        if (in_array($objType, [BlockType::TYPE_PHOTO, BlockType::TYPE_ANSWER])) {
                            //品牌自己的内容不能设置为口碑内容
                            if ($objUid != $brandUid) {
                                $noteIds[] = $val;
                            }
                        } else if ($objType == BlockType::TYPE_BLANK) {
                            //品牌自己的内容不能设置为口碑内容
                            if ($objUid != $brandUid) {
                                $blankIds[] = $val;
                            }
                        }
                    }
                }
                $noteIdCount = [];
                $blankIdCount = [];
                $noteObjId = '';
                $blankObjId = '';
                //查询交互数(点赞+收藏)最高的内容
                if ($noteIds) {
                    $noteCounterArr = PhotoCounter::getMulti($noteIds);
                    $noteInteractArr = [];
                    foreach ($noteCounterArr as $noteId => $row) {
                        $noteInteractArr[$noteId] = $row['like'] + $row['favorite'];
                    }
                    if ($noteCounterArr) {
                        arsort($noteInteractArr);
                        $keys = array_keys($noteInteractArr);
                        $noteObjId = $keys[0];
                        $noteIdCount[$noteObjId] = $noteInteractArr[$noteObjId];
                    }
                }
                if ($blankIds) {
                    $blankCounterArr = BlankCounter::getMulti($blankIds);
                    $blankInteractArr = [];
                    foreach ($blankCounterArr as $blankId => $row) {
                        $blankInteractArr[$blankId] = $row['like'] + $row['favorite'];
                    }
                    if ($blankCounterArr) {
                        arsort($blankInteractArr);
                        $keys = array_keys($blankInteractArr);
                        $blankObjId = $keys[0];
                        $blankIdCount[$blankObjId] = $blankInteractArr[$blankObjId];
                    }
                }
                if ($noteObjId && $blankObjId) {
                    $objId = ($blankIdCount[$blankObjId] > $noteIdCount[$noteObjId]) ? $blankObjId : $noteObjId;
                } else if ($noteObjId) {
                    $objId = $noteObjId;
                } else if ($blankObjId) {
                    $objId = $blankObjId;
                }
            }
        }
    }
    $objId = $objId ? $objId : '';
    if ($objId) {
        var_dump('basic Id:' . $basicId . '#查询的精选或则最大交互数内容id:' . $objId);
    } else {
        var_dump('basic Id:' . $basicId . '#未查询的精选或则最大交互数内容');
    }
    return $objId;
}
//20秒

